<?php

/**
 * This is the model class for table "salesjournal".
 *
 * The followings are the available columns in table 'salesjournal':
 * @property string $id
 * @property string $createDate
 * @property string $voucherPrefix
 * @property string $voucherPostfix
 * @property string $salesGlId
 * @property string $salesGlTransactionId
 * @property string $accountReceivableGlId
 * @property string $accountReceivableGlTransactionId
 * @property string $narration
 *
 * The followings are the available model relations:
 * @property Gl $accountReceivableGl
 * @property Gl $salesGl
 * @property Salesjournalitem[] $salesjournalitems
 */
class SalesJournal extends CActiveRecord
{
	const VOUCHER_PREFIX = 'SL';
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'salesjournal';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('createDate, voucherPrefix, voucherPostfix, salesGlId, salesGlTransactionId, accountReceivableGlId, accountReceivableGlTransactionId, narration', 'required'),
			array('voucherPrefix', 'length', 'max'=>2),
			array('voucherPostfix, salesGlTransactionId', 'length', 'max'=>10),
			array('salesGlId, accountReceivableGlId, accountReceivableGlTransactionId', 'length', 'max'=>11),
			array('narration', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, createDate, voucherPrefix, voucherPostfix, salesGlId, salesGlTransactionId, accountReceivableGlId, accountReceivableGlTransactionId, narration', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'accountReceivableGl' => array(self::BELONGS_TO, 'Gl', 'accountReceivableGlId'),
			'salesGl' => array(self::BELONGS_TO, 'Gl', 'salesGlId'),
			'salesjournalitems' => array(self::HAS_MANY, 'Salesjournalitem', 'salesJournalId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'createDate' => 'Create Date',
			'voucherPrefix' => 'Prefix',
			'voucherPostfix' => 'Postfix',
			'salesGlId' => 'Sales Gl',
			'salesGlTransactionId' => 'Sales Gl Transaction',
			'accountReceivableGlId' => 'Customer',
			'accountReceivableGlTransactionId' => 'Account Receivable Gl Transaction',
			'narration' => 'Narration',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('createDate',$this->createDate,true);
		$criteria->compare('voucherPrefix',$this->voucherPrefix,true);
		$criteria->compare('voucherPostfix',$this->voucherPostfix,true);
		$criteria->compare('salesGlId',$this->salesGlId,true);
		$criteria->compare('salesGlTransactionId',$this->salesGlTransactionId,true);
		$criteria->compare('accountReceivableGlId',$this->accountReceivableGlId,true);
		$criteria->compare('accountReceivableGlTransactionId',$this->accountReceivableGlTransactionId,true);
		$criteria->compare('narration',$this->narration,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return SalesJournal the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	protected function afterFind(){
		parent::afterFind();
		$this->createDate =  date('d-m-Y', CDateTimeParser::parse($this->createDate, 'yyyy-MM-dd')); 
	}

	protected function beforeSave(){
		if(parent::beforeSave())
		{
			$temp = date('d-m-Y', CDateTimeParser::parse($this->createDate, 'dd-MM-yyyy'));
			if($this->createDate == $temp)
				$this->createDate = date('Y-m-d', CDateTimeParser::parse($this->createDate, 'dd-MM-yyyy'));
			
			return TRUE;
		}
		else 
			return FALSE;
	}
}
